﻿using System.Web;
using Cms.Web.Framework.Infrastructure;
using iLib.Web.UI;

namespace Cms.Web.Framework.Controls
{
    public class MasterTemplateContent : Content
    {
        public MasterTemplateContent()
        {
            Class = "cmsMasterTemplateContent";
        }
        
        public override void RenderPublishedContent(RequestDetails details)
        {
            if (details.MasterTemplate != null)
            {
                string html = details.MasterTemplate.GetPublishedContentHtml(ID, details.Locale.ID);
                ContentParser.Parse(html, this, details.RequestedUrl);
            }
        }
        
        public override void RenderContent(RequestDetails details)
        {
            string html = string.Empty;
            if (details.MasterTemplate != null)
            {
                if (details.GetQueryStringValue("previewmode") == "mastertemplate" &&
                    details.ContentTranslationVersion != null)
                    html = details.MasterTemplate.GetContentHtml(ID, details.Locale.ID, details.ContentTranslationVersion.Timestamp);
                else
                    html = details.MasterTemplate.GetPublishedContentHtml(ID, details.Locale.ID);
            }
            if (html == string.Empty)
                html = "&nbsp;";
            ContentParser.Parse(html, this, details.RequestedUrl);
        }
        
        public override void RenderEditAreaLinks(RequestDetails details)
        {
            if (details.MasterTemplate == null || !details.Administrator.HasPermission.EditMasterTemplate)
            {
                return;
            }
            var content = details.MasterTemplate.GetContent(ID);
            if (content != null && content.DeniedAdministratorGroups.Contains(details.Administrator.Group))
            {
                return;
            }
            var areaMenu = new EditAreaMenu();
            areaMenu.ContentType = "MasterTemplateContent";
            areaMenu.PreviewMode = details.GetQueryStringValue("previewmode");
            areaMenu.TemplateHasNewerVersionThanPublished =
                details.MasterTemplate.HasNewerVersionThanPublished(details.Locale.ID);
            areaMenu.AreaID = ID;
            areaMenu.MasterTemplateID = details.MasterTemplate.ID;
            areaMenu.LocaleID = details.Locale.ID;
            areaMenu.VersionID = details.GetQueryStringValue("previewmode") == "mastertemplate" &&
                                 details.ContentTranslationVersion != null
                                     ? details.ContentTranslationVersion.ID
                                     : -1;
            areaMenu.SaveReturnUrl =
                HttpContext.Current.Server.UrlEncode("EditMasterTemplate.aspx?id=" + details.MasterTemplate.ID +
                                                     "&tab=content");

            // The CancelReturnUrl is determined by which page the editing is done from. 
            // It can be either EditMasterTemplate.aspx, EditDetailTemplate.aspx, EditPage.aspx or EditApplicationPage.aspx.
            if (details.GetQueryStringValue("previewmode") == "mastertemplate")
                areaMenu.CancelReturnUrl =
                    HttpContext.Current.Server.UrlEncode("EditMasterTemplate.aspx?id=" + details.MasterTemplate.ID +
                                                         "&tab=content");
            else if (details.GetQueryStringValue("previewmode") == "detailtemplate")
                areaMenu.CancelReturnUrl =
                    HttpContext.Current.Server.UrlEncode("EditDetailTemplate.aspx?id=" + details.DetailTemplate.ID +
                                                         "&tab=content");
            else if (Url.Page.ToLower() == "sitepreview.aspx")
                areaMenu.CancelReturnUrl =
                    HttpContext.Current.Server.UrlEncode("EditPage.aspx?id=" + details.Page.ID + "&tab=content");
            else
                areaMenu.CancelReturnUrl =
                    HttpContext.Current.Server.UrlEncode("EditApplicationPage.aspx?id=" + details.Page.ID +
                                                         "&tab=content");
            Controls.Add(areaMenu);
        }
    }
}